package com.mdd.front.service.impl.yangyangLeadingEnterprise;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppCorporateFinance;
import com.mdd.common.entity.yangyangLeadingEnterprise.common.JsonReturn;
import com.mdd.common.entity.yangyangLeadingEnterprise.param.CorporateFinanceParam;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppCorporateFinanceMapper;
import com.mdd.front.service.yangyangLeadingEnterprise.ITAppCorporateFinanceService;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppCorporateFinanceCreateValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppCorporateFinanceSearchValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppCorporateFinanceUpdateValidate;
import com.mdd.front.vo.yangyangLeadingEnterprise.TAppCorporateFinanceDetailVo;
import com.mdd.front.vo.yangyangLeadingEnterprise.TAppCorporateFinanceListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
 * 企业融资实现类
 * @author LikeAdmin
 */
@Service
public class TAppCorporateFinanceServiceImpl implements ITAppCorporateFinanceService {
        
    @Resource
    TAppCorporateFinanceMapper tAppCorporateFinanceMapper;

    /**
     * 企业融资列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TAppCorporateFinanceListedVo>
     */
    @Override
    public PageResult<TAppCorporateFinanceListedVo> list(PageValidate pageValidate, TAppCorporateFinanceSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TAppCorporateFinance> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        tAppCorporateFinanceMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:brandPic@brand_pic:str",
            "like:name:str",
            "=:maxLimit@max_limit:int",
            "=:maxPeriods@max_periods:int",
            "=:annualizedInterestRate@annualized_interest_rate:long",
            "=:areaId@area_id:int",
            "=:type:int",
            "=:creditTerm@credit_term:str",
            "=:repaymentMethod@repayment_method:str",
        });

        IPage<TAppCorporateFinance> iPage = tAppCorporateFinanceMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TAppCorporateFinanceListedVo> list = new LinkedList<>();
        for(TAppCorporateFinance item : iPage.getRecords()) {
            TAppCorporateFinanceListedVo vo = new TAppCorporateFinanceListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 企业融资详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TAppCorporateFinance
     */
    @Override
    public TAppCorporateFinanceDetailVo detail(Integer id) {
        TAppCorporateFinance model = tAppCorporateFinanceMapper.selectOne(
                new QueryWrapper<TAppCorporateFinance>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TAppCorporateFinanceDetailVo vo = new TAppCorporateFinanceDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 企业融资新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TAppCorporateFinanceCreateValidate createValidate) {
        TAppCorporateFinance model = new TAppCorporateFinance();
        model.setBrandPic(createValidate.getBrandPic());
        model.setName(createValidate.getName());
        model.setMaxLimit(createValidate.getMaxLimit());
        model.setMaxPeriods(createValidate.getMaxPeriods());
        model.setAnnualizedInterestRate(createValidate.getAnnualizedInterestRate());
        model.setType(createValidate.getType());
        model.setCreditTerm(createValidate.getCreditTerm());
        model.setRepaymentMethod(createValidate.getRepaymentMethod());
        tAppCorporateFinanceMapper.insert(model);
    }

    /**
     * 企业融资编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TAppCorporateFinanceUpdateValidate updateValidate) {
        TAppCorporateFinance model = tAppCorporateFinanceMapper.selectOne(
                new QueryWrapper<TAppCorporateFinance>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setBrandPic(updateValidate.getBrandPic());
        model.setName(updateValidate.getName());
        model.setMaxLimit(updateValidate.getMaxLimit());
        model.setMaxPeriods(updateValidate.getMaxPeriods());
        model.setAnnualizedInterestRate(updateValidate.getAnnualizedInterestRate());
        model.setType(updateValidate.getType());
        model.setCreditTerm(updateValidate.getCreditTerm());
        model.setRepaymentMethod(updateValidate.getRepaymentMethod());
        tAppCorporateFinanceMapper.updateById(model);
    }

    /**
     * 企业融资删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TAppCorporateFinance model = tAppCorporateFinanceMapper.selectOne(
                new QueryWrapper<TAppCorporateFinance>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tAppCorporateFinanceMapper.delete(new QueryWrapper<TAppCorporateFinance>().eq("id", id));
    }

    /**
     * 企业融资列表
     * @param param
     * @return
     */
    @Override
    public List<TAppCorporateFinance> selectList(CorporateFinanceParam param) {
        String name = param.getName();
//        Integer areaId = param.getAreaId();
        String provinceCode = param.getProvinceCode();
        String cityCode = param.getCityCode();
        String areaCode = param.getAreaCode();
        Integer type = param.getType();
        QueryWrapper<TAppCorporateFinance> wrapper = new QueryWrapper<>();
        if(name != null && !name.isEmpty()) {
            wrapper.like("name", "%"+name+"%");
        }
        if(type != null) {
            wrapper.eq("type", type);
        }
        if(provinceCode!=null && !provinceCode.isEmpty()){
            wrapper.eq("province_code", provinceCode);
        }
        if(cityCode!=null && !cityCode.isEmpty()){
            wrapper.eq("city_code", cityCode);
        }
        if(areaCode!=null && !areaCode.isEmpty()){
            wrapper.eq("area_code", areaCode);
        }
        List<TAppCorporateFinance> selectList = tAppCorporateFinanceMapper.selectList(wrapper);
        return Optional.ofNullable(selectList).orElse(new ArrayList<>());
    }

    @Override
    public TAppCorporateFinance selectById(Integer id) {
        TAppCorporateFinance select = tAppCorporateFinanceMapper.selectById(id);
        return Optional.ofNullable(select).orElse(new TAppCorporateFinance());
    }

}
